#include "endrawcontext.h"
#include <QGraphicsEllipseItem>

EnDrawContext::EnDrawContext()
{
    view = new EnGraphicsView();
    view->setGeometry( 50, 44, 960, 720 );
    view->show();
}

EnDrawContext::~EnDrawContext()
{
    delete view;
}

/*
void EnDrawContext::drawFrame()
{
    view->update();
    qDebug() << "Test";
}

void EnDrawContext::logicFrame()
{

}
*/

EnGraphicsView* EnDrawContext::getView()
{
    return this->view;
}


void EnDrawContext::loadModule( EnModule *m )
{
    if( !module )
        QObject::disconnect( module, SIGNAL( bodyLocallyAdded(EnBody*) ), this, SLOT( addBody(EnBody*) ) );

    this->module = m;
    this->view->setModule( m );

    view->scene()->setBackgroundBrush( *module->getBackgroundBrush() );

    QHashIterator<QString,EnBody*> i( module->bodyMap );
    while( i.hasNext() )
    {
        i.next();
        view->scene()->addItem( i.value()->getGraphicsItem() );
    }

    QObject::connect( module, SIGNAL( bodyLocallyAdded(EnBody*) ), this, SLOT( addBody(EnBody*) ) );
}

/*
void EnDrawContext::start()
{
    drawTimer.start();
    logicTimer.start();
}
*/

EnModule* EnDrawContext::getModule()
{
    return module;
}

void EnDrawContext::addBody( EnBody *body )
{
    view->scene()->addItem( body->getGraphicsItem() );
}
